using System;
using System.Diagnostics.Contracts;

namespace Granite.Data
{
	/// <summary>
	/// Contains tracking information about procedure calls
	/// </summary>
	public class ProcedureCallEventArgs : EventArgs
	{
		readonly DateTimeOffset? m_EndTime;

		readonly Exception m_Error;

		readonly ProcedureCall m_Procedure;

		readonly DateTimeOffset m_StartTime;

		/// <summary>
		/// Contains tracking information about procedure calls
		/// </summary>
		/// <param name="procedure"></param>
		/// <param name="startTime"></param>
		/// <param name="endTime">optional</param>
		/// <param name="error">optional</param>
		public ProcedureCallEventArgs(ProcedureCall procedure, DateTimeOffset startTime, DateTimeOffset? endTime = null, Exception error = null)
		{
			if (procedure == null)
				throw new ArgumentNullException("procedure", "procedure is null.");
			Contract.EndContractBlock();

			m_Error = error;
			m_EndTime = endTime;
			m_StartTime = startTime;
			m_Procedure = procedure;
		}

		/// <summary>
		/// Time when the procedure failed or completed
		/// </summary>
		public DateTimeOffset? EndTime
		{
			get { return m_EndTime; }
		}

		/// <summary>
		/// Exception from a failed procedure call
		/// </summary>
		public Exception Error
		{
			get { return m_Error; }
		}

		/// <summary>
		/// Procedure that is being executed
		/// </summary>
		public ProcedureCall Procedure
		{
			get { return m_Procedure; }
		}

		/// <summary>
		/// Time the procedure was initiated
		/// </summary>
		public DateTimeOffset StartTime
		{
			get { return m_StartTime; }
		}

	}
}
